package api

import (
	"api-gateway/basic/inits"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"context"
	"github.com/gin-gonic/gin"
)

// BrandAdd
// @Summary      添加品牌
// @Description  添加一个新的品牌
// @Tags         品牌模块
// @Accept       json
// @Produce      json
// @Param        brand  body      request.BrandAdd  true  "品牌参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /brand/brandAdd [post]
func BrandAdd(c *gin.Context) {
	var req request.BrandAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	add, err := inits.UserCline.BrandAdd(context.Background(), &__.BrandAddReq{
		BrandName:        req.BrandName,
		BrandLogo:        req.BrandLogo,
		BrandDescription: req.BrandDescription,
	})
	if err != nil {
		response.Error(c, 500, "品牌添加失败", err.Error())
		return
	}
	response.Success(c, "添加成功", add.Id)
}

// BrandDel
// @Summary      品牌删除
// @Description  删除一个品牌
// @Tags         品牌模块
// @Accept       json
// @Produce      json
// @Param        brand  body      request.BrandDel  true  "品牌参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /brand/brandDel [post]
func BrandDel(c *gin.Context) {
	var req request.BrandDel
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.UserCline.BrandDel(context.Background(), &__.BrandDelReq{
		Id: uint64(req.Id),
	})
	if err != nil {
		response.Error(c, 500, "品牌删除失败", err.Error())
		return
	}
	response.Success(c, "品牌删除成功", nil)
}

// BrandFind
// @Summary      品牌展示
// @Description  展示一个品牌
// @Tags         品牌模块
// @Accept       json
// @Produce      json
// @Param        brand  query      request.BrandFind  true  "品牌参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /brand/brandFind [get]
func BrandFind(c *gin.Context) {
	var req request.BrandFind
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	find, err := inits.UserCline.BrandFind(context.Background(), &__.BrandFindReq{})
	if err != nil {
		response.Error(c, 500, "品牌展示失败", err.Error())
		return
	}
	response.Success(c, "品牌展示成功", find)
}

// BrandUpdate
// @Summary      品牌修改
// @Description  修改一个品牌
// @Tags         品牌模块
// @Accept       json
// @Produce      json
// @Param        brand  body      request.BrandUpdate  true  "品牌参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /brand/brandUpdate [post]
func BrandUpdate(c *gin.Context) {
	var req request.BrandUpdate
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	update, err := inits.UserCline.BrandUpdate(context.Background(), &__.BrandUpdateReq{
		Id:               uint64(req.Id),
		BrandName:        req.BrandName,
		BrandLogo:        req.BrandLogo,
		BrandDescription: req.BrandDescription,
	})
	if err != nil {
		response.Error(c, 500, "品牌修改失败", err.Error())
		return
	}
	response.Success(c, "品牌修改成功", update.Id)
}

// BrandUpdate
// @Summary      品牌详情
// @Description  展示品牌的数据
// @Tags         品牌模块
// @Accept       json
// @Produce      json
// @Param        brand  query   request.BrandDetails  true  "品牌参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /brand/brandDetails [get]
func BrandDetails(c *gin.Context) {
	var req request.BrandDetails
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	details, err := inits.UserCline.BrandDetails(context.Background(), &__.BrandDetailsReq{
		Id: uint64(req.Id),
	})
	if err != nil {
		response.Error(c, 500, "品牌详情展示失败", err.Error())
		return
	}
	response.Success(c, "品牌详情展示成功", details)
}
